package binarytree;

/**
 * @ClassName RecursiveBinaryTree
 * @Description TODO
 * @Author Admin
 * @Date 2021/12/15 16:59
 * @Version 1.0
 * 用递归，实现先序，代码简单，容易理解，但效率不高
 */

public class RecursiveBinaryTree {
    public static void main(String[] args) {
        BinaryTreeNode node = new BinaryTreeNode(1);
        node.left=new BinaryTreeNode(2);
        node.right=new BinaryTreeNode(3);
        node.left.left=new BinaryTreeNode(4);
        node.left.right=new BinaryTreeNode(5);
        node.right.left=new BinaryTreeNode(6);
        node.right.right=new BinaryTreeNode(7);
        preOrder(node);
        System.out.println("******栈实现先序遍历*********");
        UnRecursiveBinaryTree.preOrder(node);
        System.out.println("******中序遍历*********");
        inOrder(node);
        System.out.println("******栈实现中序遍历*********");
        UnRecursiveBinaryTree.inOrder(node);
        System.out.println("******后序遍历*********");
        postOrder(node);
        System.out.println("******栈实现后序遍历*********");
        UnRecursiveBinaryTree.postOrder(node);


    }
    //递归实现二叉树先续遍历
    public static void preOrder(BinaryTreeNode node){
        if (node==null)
            return;
        System.out.println(node.value);
        preOrder(node.left);
        preOrder(node.right);
    }

    //递归实现二叉树中续遍历
    public static void inOrder(BinaryTreeNode node){
        if (node==null)
            return;
        inOrder(node.left);
        System.out.println(node.value);
        inOrder(node.right);
    }
    //递归实现二叉树后续遍历
    public static void postOrder(BinaryTreeNode node){
        if (node==null)
            return;
        postOrder(node.left);

        postOrder(node.right);

        System.out.println(node.value);
    }


}
